/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.niclun.projectwebshop.nbp.DataBase;

import edu.chl.niclun.projectwebshop.nbp.Entities.Actor;
import edu.chl.niclun.projectwebshop.nbp.Entities.Movie;
import edu.chl.niclun.projectwebshop.nbp.Entities.Movie.Genre;
import java.io.InputStream;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Class to import Movies from a .txt and make them into MovieObjects
 * @author Nume
 */
public class ImportMovies {
    private Scanner src;
   
    private ArrayList<Movie> movies;
    
    public ImportMovies(){
      movies = new ArrayList<Movie>();
        
        
        createInput();
        readMovies();
        
    }
    //Open inpustream to the .txtfile
    public void createInput(){
        InputStream in = ImportMovies.class.getResourceAsStream("/MovieDB.txt");
        src = new Scanner(in);
    }
    
    public ArrayList<Movie> getMovies(){
        return movies;
    }
    
    //check if the actor already exists. If it does return it. If not return null
    private Actor findActor(String fname, String lname){
             for(Movie movie : movies){
                 List<Actor> actors = movie.getActors();
                 for(Actor actor : actors){
                     if(actor.getFirstName().equals(fname) && actor.getLastName().equals(lname))
                         return actor;
                 }
             }
             return null;
         }
    
    //read line by line from the .txtfile
    public void readMovies(){
        
        //continue reading if we have another line in the txtfile
        while(src.hasNextLine()){
            
            //create movie
           String titel = src.nextLine();
           String director = src.nextLine();
           Movie mov = new Movie(titel,director);
           movies.add(mov);
           
           //set description
           mov.setDescription(src.nextLine());
           
           //sets actors
           ArrayList<Actor> actorList = new ArrayList<Actor>();
           
           String[] actors = src.nextLine().split(",");
           for(String actor : actors){
                String[] firstNameLastName = actor.split(";");
                    if(firstNameLastName.length == 2){
                        //check if the actor already exists in another movie...
                        Actor act = findActor(firstNameLastName[0], firstNameLastName[1]) ;
                        //if not..
                        if(act == null){
                            //create a new one
                            act = new Actor(firstNameLastName[0], firstNameLastName[1]);
                        }
                        actorList.add(act);
                        act.addMovie(mov);
                    }
           }
           mov.setActors(actorList);
           
           //set the picture
           mov.setMovieCover(src.nextLine());
           
           //set productionyear
           mov.setProdYear(Integer.parseInt(src.nextLine()));
            
           //set rating
           mov.setRating(Double.parseDouble(src.nextLine()));
           mov.setAddedToDb(new Date());
           
           mov.setPrice(Integer.parseInt(src.nextLine()));
           
           //set genre
           ArrayList<Genre> genreList = new ArrayList<Genre>();
           mov.setGenres(genreList);
           String[] genres = src.nextLine().split(",");
           for(String genre : genres){
               //make the strings into enums
             genreList.add(Genre.valueOf(genre));
               
           }
         
           
           //read the separator between the different movies. do nothing
           src.nextLine();
        }
        
        
    }
    
    
}
